package cms.simmytech.service;

import cms.simmytech.mapper.TbCategoryMapper;
import cms.simmytech.model.TbCategory;
import cms.simmytech.model.TbCategory;
import cms.simmytech.model.TbCategoryExample;
import cms.simmytech.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private TbCategoryMapper  mapper;

    public PageInfo<TbCategory> getPageList(Integer pageNum, Integer pageSize, String name, Byte cateType, Byte cateState) {
        TbCategoryExample example = new TbCategoryExample();
        TbCategoryExample.Criteria criteria = example.createCriteria();
        if(!"".equals(name)&&null!=name){
            criteria.andCateNameLike("%"+name+"%")	;
        }
        if (cateType!=-99){
            criteria.andCateTypeEqualTo(cateType);
        }
        if (cateState!=-99){
            criteria.andCateStateEqualTo(cateState);
        }
        example.setOrderByClause("cate_order asc,cate_id  desc");
        PageHelper.startPage(pageNum, pageSize);
        List<TbCategory> list = mapper.selectByExample(example);
        PageInfo<TbCategory> pageInfo = new PageInfo<TbCategory>(list);
        return pageInfo;
    }

    public TbCategory getBean(Integer id) {
        return  mapper.selectByPrimaryKey(id);
    }

    public void save(TbCategory bean) {
        if (bean.getCateId()== null) {
            Integer now = DateUtil.getIntStampTime();
            bean.setCreateTime(now);
            mapper.insertSelective(bean);

        } else {
            mapper.updateByPrimaryKeySelective(bean);
        }
    }

    public void delete(int id) {
        mapper.deleteByPrimaryKey(id);
    }

    public List<TbCategory> getAllList(Byte type) {
        TbCategoryExample example=new TbCategoryExample();
        example.createCriteria().andCateTypeEqualTo(type);
      return   mapper.selectByExample(example);
    }
}
